package com.jswdwsx.esign.controller

import com.jswdwsx.esign.cloud.request.MerchantInfoReq
import com.jswdwsx.esign.cloud.request.SignReq
import com.jswdwsx.esign.cloud.response.FlowStatusResp
import com.jswdwsx.esign.cloud.response.WebResult
import com.jswdwsx.esign.commom.ResEnum
import com.jswdwsx.esign.protocol.response.QueryFileTemplateResp
import com.jswdwsx.esign.service.RequestService
import com.jswdwsx.esign.service.SignService
import com.jswdwsx.esign.util.json
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.slf4j.LoggerFactory
import org.springframework.web.bind.annotation.*

@Api(tags = ["签署接口（业务系统调用）"])
@RestController
@RequestMapping("/v1/api/signs")
class SignController(
    private val signService: SignService,
    private val requestService: RequestService
) {

    private val logger = LoggerFactory.getLogger(SignController::class.java)

    @GetMapping("params")
    @ApiOperation(value = "查询模板参数", notes = "根据模板查询模板参数")
    fun paramQuery(@RequestParam flowTemplateId: String): WebResult<List<QueryFileTemplateResp>> {
        return ResEnum.success(requestService.queryParams(flowTemplateId))
    }

    @PostMapping("merchant")
    @ApiOperation(value = "初始化商户信息", notes = "商户入驻时为商户创建E签宝的各种信息，创建代理人，创建企业，创建印章，返回印章")
    fun initOrg(@RequestBody merchantInfoReq: MerchantInfoReq): WebResult<String?> {
        logger.info("创建商户接口入参:${merchantInfoReq.json()}")
        val url = signService.initMerchant(merchantInfoReq)
        logger.info("创建商户接口出参:${url}")
        return ResEnum.success(url)
    }

    @PostMapping
    @ApiOperation(value = "发起签署接口", notes = "根据模板和签署人等信息创建一个签署请求")
    fun sign(@RequestBody signReq: SignReq): WebResult<String?> {
        logger.info("发起签署接口入参:${signReq.json()}")
        val url = signService.sign(signReq)
        logger.info("发起签署接口出参:${url}")
        return ResEnum.success(url)
    }

    @GetMapping
    @ApiOperation(value = "查询签署状态", notes = "根据appId和流水号查询签署状态")
    fun signQuery(appId: String, appSerialNumber: String): WebResult<FlowStatusResp?> {
        return ResEnum.success(signService.signQuery(appId, appSerialNumber))
    }
}
